/**
 * 
 */
package xx.teach.entity;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.sql.RowSet;

import xx.db.QueryRunner;
import xx.db.annotation.Column;
import xx.db.annotation.TableName;
import xx.db.sql.Owner;
import xx.db.sql.Table;

/**
 * @author Administrator 2014-3-18
 */
@TableName(name = "t_job")
public class Job extends Table
{

    /**
     * @param qr
     * @param owner
     */
    public Job(QueryRunner qr, Owner owner)
    {
        super(qr, owner);
        // TODO Auto-generated constructor stub
    }

    public static final String JOB_ID = "job_id";

    @Column(name = JOB_ID, pk = true)
    private long jobId;

    public static final String GROUP_ID = "group_id";

    @Column(name = GROUP_ID)
    private long groupId;

    public static final String SUBMIT_TIME = "submit_time";

    @Column(name = SUBMIT_TIME)
    private Timestamp submitTime;

    public static final String TYPE = "type";

    @Column
    private int type;

    public static final String NAME = "name";

    @Column
    private String name;

    public static final String CONTENT = "content";

    @Column
    private String content;

    public static final String ATTACH_PATH = "attach_path";

    @Column(name = ATTACH_PATH)
    private String attachPath;

    public static final String TEACHER_ID = "teacher_id";

    @Column(name = TEACHER_ID)
    private long teacherId;

    public long getJobId()
    {
        return jobId;
    }

    public Job setJobId(long jobId)
    {
        this.jobId = jobId;
        return this;
    }

    public long getGroupId()
    {
        return groupId;
    }

    public Job setGroupId(long groupId)
    {
        this.groupId = groupId;
        return this;
    }

    public Timestamp getSubmitTime()
    {
        return submitTime;
    }

    public Job setSubmitTime(Timestamp submitTime)
    {
        this.submitTime = submitTime;
        return this;
    }

    public int getType()
    {
        return type;
    }

    public Job setType(int type)
    {
        this.type = type;
        return this;
    }

    public String getName()
    {
        return name;
    }

    public Job setName(String name)
    {
        this.name = name;
        return this;
    }

    public String getContent()
    {
        return content;
    }

    public Job setContent(String content)
    {
        this.content = content;
        return this;
    }

    public String getAttachPath()
    {
        return attachPath;
    }

    public Job setAttachPath(String attachPath)
    {
        this.attachPath = attachPath;
        return this;
    }

    public long getTeacherId()
    {
        return teacherId;
    }

    public Job setTeacherId(long teacherId)
    {
        this.teacherId = teacherId;
        return this;
    }

    /*
     * (non-Javadoc)
     * @see xx.db.sql.Table#delete()
     */
    @Override
    public int delete() throws SQLException
    {
        // 删除相关的answer
        RowSet rs = select.from(Answer.class, Answer.ANSWER_ID).where(Answer.JOB_ID + "=" + jobId).query();
        while (rs.next())
        {
            Answer answer = new Answer(qr, owner);
            answer.setAnswerId(rs.getLong(Answer.ANSWER_ID)).delete();
        }

        return super.delete();
    }

    /**
     * 根据type和groupId获取job列表
     * 
     * @return
     * @throws SQLException
     */
    public List<Job> getJobByGroupIdAndType() throws SQLException
    {
        if (type == 0)
            return null;

        return select.from(Job.class).where(Job.GROUP_ID + "=" + groupId + " and " + Job.TYPE + "=" + type).desc(Job.SUBMIT_TIME).go();
    }

    /**
     * 获取堂上最新布置的作业
     * 
     * @return
     * @throws SQLException
     */
    public Job getJobOnclass() throws SQLException
    {
        List<Job> jobList = select.from(Job.class).where(Job.TYPE + "=1").desc(Job.SUBMIT_TIME).go();
        if (jobList.size() == 0)
            return null;
        else
            return jobList.get(0);
    }
}
